package com.dajingling.controller;

import com.dajingling.bean.*;
import com.dajingling.mvc.ResponseBody;
import com.dajingling.service.EUserExpressService;
import com.dajingling.service.UserAdminService;
import com.dajingling.util.DateFormatUtil;
import com.dajingling.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserAdminController {
    @ResponseBody("/admin/views/console_user.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Integer> list = UserAdminService.console();
        Message msg = new Message();
        if (list.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(list);
        String json = JSONUtil.toJSON(msg);
        System.out.println("控制台：" + json);
        return json;
    }

    @ResponseBody("/admin/views/user/findAll.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        //1.获取查询数据的索引
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<UserAdmin> list = UserAdminService.findAll(true, offset, pageNumber);
        List<BootStrapTableUser> list1 = new ArrayList<>();

        for (UserAdmin e : list) {
            String logonTime = DateFormatUtil.format(e.getLogonTime());
            String loginTime = e.getLoginTime() == null ? "未登录" : DateFormatUtil.format(e.getLoginTime());
            BootStrapTableUser e2 = new BootStrapTableUser(e.getUid(), e.getUserName(), e.getPassword(), e.getUserPhone(), e.getIdCard(), "", logonTime, loginTime, e.getLoginIp());
            list1.add(e2);
        }

        Map<String, Integer> console = EUserExpressService.console();
        int total = console.get("data_size");
        //4.把集合封装为bootstrap-table(前端的表格)能识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println("用户列表：" + json);
        return json;
    }

    @ResponseBody("/admin/views/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("username");
        String userPhone = request.getParameter("userphone");
        String idCard = request.getParameter("idcard");
        String password = request.getParameter("password");
        UserAdmin e = new UserAdmin(userName, password, userPhone, idCard);
        boolean flag = UserAdminService.insert(e);
        Message msg = new Message();
        if (flag) {
            //录入成功
            msg.setStatus(0);
            msg.setResult("用户录入成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("用户录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("用户录入：" + json);
        return json;
    }

    @ResponseBody("/admin/views/user/findByUserPhone.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userphone");
        UserAdmin e = UserAdminService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (e == null) {
            msg.setStatus(-1);
            msg.setResult("号码不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("查询用户：" + json);
        return json;
    }

    @ResponseBody("/admin/views/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        String oldUserPhone = request.getParameter("olduserphone");
        String userName = request.getParameter("username");
        String userPhone = request.getParameter("userphone");
        String idCard = request.getParameter("idcard");
        String password = request.getParameter("password");
        UserAdmin e = new UserAdmin(userName, password, userPhone, idCard);
        boolean flag = UserAdminService.update(oldUserPhone, e);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("修改用户：" + json);
        return json;
    }

    @ResponseBody("/admin/views/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userphone");
        boolean flag = UserAdminService.delete(userPhone);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("删除快递：" + json);
        return json;
    }
}